home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 October / EnigmA AMIGA RUN 01 (1995)(G.R. Edizioni)(IT)[!][issue 1995-10][Aminet 7].iso / Aminet / mus / play / tracker_4_31.lzh / tracker / Arch / Hpux / low_audio.c < prev   
C/C++ Source or Header  |  1995-05-15  |  5KB  |  202 lines

  1. /* hplow_audio.c 
  2.     vi:ts=3 sw=3:
  3.  */
  4.  
  5. /* Driver for HP9000 series 710/7x5 with HP-UX 9.01 */
  6. /* Using low-level audio calls */
  7. /* Copyright 1993 Marc Espie   (Marc.Espie@ens.fr)  */
  8. /* Copyright 1993 Markus Gyger (mgyger@itr.ch) */
  9.  
  10. #include "defs.h"
  11. #include <fcntl.h>
  12. #include <unistd.h>
  13. #include <sys/audio.h>
  14. #include "extern.h"
  15.  
  16. #define DEFAULT_BUFFERS
  17. #define DEFAULT_SET_MIX
  18. #include "Arch/common.c"
  19.  
  20. ID("$Id: low_audio.c,v 1.4 1995/05/15 12:19:31 espie Exp espie $")
  21. #ifndef DEFAULT_SAMPLE_RATE
  22. #define DEFAULT_SAMPLE_RATE  22050
  23. #endif
  24.  
  25. #ifndef AUDIO_NAME
  26. #define AUDIO_NAME           "/dev/audio"
  27. #endif
  28. #ifndef AUDIO_CTL_NAME
  29. #define AUDIO_CTL_NAME       "/dev/audioCtl"
  30. #endif
  31.  
  32. LOCAL int audio;
  33. LOCAL int audio_ctl;
  34.  
  35. LOCAL struct audio_describe ainfo;
  36. LOCAL int sample_rate;
  37. LOCAL int channels;
  38. LOCAL int min_samples;
  39.  
  40.  
  41. LOCAL int available(f)
  42. int f;
  43.     {
  44.     int best = 0;
  45.     int i;
  46.  
  47.     for (i = 0; i < ainfo.nrates; i++)
  48.     if (abs(ainfo.sample_rate[i] - f) < abs(best - f))
  49.         best = ainfo.sample_rate[i];
  50.     return best;
  51.     }
  52.  
  53. int open_audio(f, s)
  54. int f;
  55. int s;
  56.     {
  57.     int type;
  58.  
  59.     audio_ctl = open(AUDIO_CTL_NAME, O_WRONLY | O_NDELAY);
  60.     if (audio_ctl == -1)
  61.         {
  62.         end_all("No audio control device");
  63.         }
  64.  
  65.     if (ioctl(audio_ctl, AUDIO_DESCRIBE, &ainfo) == -1)
  66.         {
  67.     end_all("No audio info");
  68.         }
  69.  
  70.     if (f == 0)
  71.         f = DEFAULT_SAMPLE_RATE;
  72.         /* round frequency to acceptable value */
  73.     sample_rate = available(f);
  74.  
  75.         /* check whether we have stereo device */
  76.     if (ainfo.nchannels < 2)
  77.         {
  78.             /* a 710 or 425 -> revert to base quality audio */
  79.         stereo = 0;
  80.         channels = 1;
  81.         }
  82.     else
  83.         {
  84.             /* 7x5 set up */
  85.         stereo = s;
  86.         if (stereo)
  87.             {
  88.             channels = 2;
  89.             set_mix(30);
  90.             }
  91.         else
  92.             channels = 1;
  93.         }
  94.  
  95.         /* write a minimum of samples to avoid underflows on 715 (???) */
  96.     switch (ainfo.audio_id)
  97.         {
  98.         case AUDIO_ID_CS4215:
  99.             /* empirically found values */
  100.             min_samples = 12288;
  101.             if (channels < 2) min_samples = 8192;
  102.             if (sample_rate <= 11025) min_samples = 6144;
  103.             if (sample_rate <= 8000) min_samples = 4096;
  104.             break;
  105.  
  106.         case AUDIO_ID_PSB2160:
  107.         default:
  108.             min_samples = 0;
  109.             break;
  110.         }
  111.  
  112.     if (ioctl(audio_ctl, AUDIO_SET_DATA_FORMAT, AUDIO_FORMAT_LINEAR16BIT) == -1)
  113.         {
  114.           end_all("Linear format not available");
  115.         }
  116.     if (ioctl(audio_ctl, AUDIO_SET_CHANNELS, channels) == -1)
  117.         {
  118.           end_all("Could not set # of audio channels");
  119.         }
  120.     if (ioctl(audio_ctl, AUDIO_SET_SAMPLE_RATE, sample_rate) == -1)
  121.         {
  122.           end_all("Could not set audio sample rate");
  123.         }
  124.  
  125.     audio = open(AUDIO_NAME, O_WRONLY | O_NDELAY);
  126.     if (audio == -1)
  127.         {
  128.           end_all("Could not open audio device");
  129.         }
  130.  
  131. #ifdef SET_OUTPUT
  132.         /* ensure we hear something */
  133.     if (ioctl(audio_ctl, AUDIO_SET_OUTPUT,
  134.         ((SET_OUTPUT & 1) ? AUDIO_OUT_INTERNAL : 0) |   /* speaker */
  135.         ((SET_OUTPUT & 2) ? AUDIO_OUT_EXTERNAL : 0) |   /* phones  */
  136.         ((SET_OUTPUT & 4) ? AUDIO_OUT_LINE : 0) |   /* line    */
  137.         ((SET_OUTPUT & 7) ? 0 : AUDIO_OUT_NONE)) == -1)
  138.           notice("Warning: could not set audio output");
  139. #endif
  140.  
  141.     idx = 0;
  142.     buffer16 = (short *)malloc(2 * channels * sample_rate);
  143.     buffer = (char *)buffer16;
  144.     if (!buffer)
  145.         end_all("Could not allocate buffer");
  146.     return sample_rate;
  147.     }
  148.  
  149. void set_synchro(s)
  150. int s;
  151.     {
  152.     }
  153.  
  154. int update_frequency()
  155.     {
  156.     int oldfreq;
  157.  
  158.     oldfreq = sample_rate;
  159.     if (ioctl(audio, AUDIO_GET_SAMPLE_RATE, &sample_rate) != -1)
  160.         {
  161.         if (oldfreq != sample_rate)
  162.             {
  163.             buffer16 = (short *)realloc(buffer16, 2 * channels * sample_rate);
  164.             buffer = (char *)buffer16;
  165.             return sample_rate;
  166.             }
  167.         }
  168.     return 0;
  169.     }
  170.  
  171. void output_samples(left, right)
  172. int left, right;
  173.     {
  174.    if (stereo)
  175.         add_samples16_stereo(left, right);
  176.     else
  177.         buffer16[idx++] = left + right;
  178.     }
  179.  
  180. void flush_buffer()
  181.     {
  182.     if (idx >= min_samples)
  183.         {
  184.         write(audio, buffer, 2 * idx);
  185.         idx = 0;
  186.         }
  187.     }
  188.  
  189. void discard_buffer()
  190.     {
  191.     if (ioctl(audio, AUDIO_RESET, RESET_TX_BUF) == -1)
  192.         notice("Warning: could not discard audio buffer");
  193.     }
  194.  
  195. void close_audio()
  196.     {
  197.     free(buffer);
  198.     close(audio);
  199.     close(audio_ctl);
  200.     }
  201.  
  202.